package com.iretailer.report.quartz;

import com.iretailer.report.influxdb.dao.InfluxdbBaseDao;
import com.iretailer.report.influxdb.dao.InfluxdbDao;
import com.iretailer.report.model.AlarmEasModel;
import com.iretailer.report.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.dto.Point;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Program: report
 * @Description:
 * @Author: Bennie
 * @Date: 2021-02-04 15:07
 **/
@Slf4j
@Component("easAlarmSummary")
public class EasAlarmSummary {

    @Value("${influxdb.dbName}")
    private String dbName;
    @Value("${influxdb.url}")
    private String url;

    private final InfluxdbBaseDao influxdbBaseDao;
    private final InfluxdbDao influxdbDao;

    public EasAlarmSummary(InfluxdbBaseDao influxdbBaseDao, InfluxdbDao influxdbDao) {
        this.influxdbBaseDao = influxdbBaseDao;
        this.influxdbDao = influxdbDao;
    }


    public void execute() {
        LocalDateTime endTime = LocalDateTime.now();
        LocalDateTime startTime = endTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
        summary(TimeUtil.localDateTime_format(startTime), TimeUtil.localDateTime_format(endTime));
    }

    public void summary(List<String> dates, List<String> devices) {
        for (String date : dates) {
            summary(date + " 00:00:00", date + " 23:59:59", devices);
        }
    }

    public void summary(String startTime, String endTime) {
        summary(startTime, endTime, null);
    }

    public void summary(String startTime, String endTime, List<String> devices) {
        log.info("------------------开始汇总EAS设备报警记录-----------------\n{}-{}", startTime, endTime);
        String tz = "Asia/Shanghai";
        List<AlarmEasModel> alarmEasModels = influxdbDao.selectAlarmEas(tz, startTime, endTime, devices);
        Map<String, List<AlarmEasModel>> modelMap = alarmEasModels.stream().collect(Collectors.groupingBy(AlarmEasModel::getKey));
        List<Point> points = modelMap.keySet().stream().map(key -> {
            String[] s = key.split("_");
            List<AlarmEasModel> models = modelMap.get(key);
            LocalDateTime dateTime = LocalDateTime.parse(s[1] + String.format("%02d", Integer.parseInt(s[2]) * 4),
                    DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            ZoneId zone = ZoneId.systemDefault();
            Instant instant = dateTime.atZone(zone).toInstant();
            return Point.measurement("eas_alarm_summary")
                    .time(instant.toEpochMilli(), TimeUnit.MILLISECONDS)
                    .tag("device_id", s[0])
                    .addField("alarm_times", models == null || models.isEmpty() ? 0 : 1)
                    .build();
        }).collect(Collectors.toList());
        if (!points.isEmpty()) {
            influxdbBaseDao.insert(url, dbName, points);
        }
        log.info("共同步了 {} 条 EAS设备报警记录", points.size());
        log.info("------------------EAS设备报警记录汇总完成------------------");
    }
}
